<?php
namespace common\models\cart;

use common\models\goods\Goods;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

/**
 * This is the model class for table "{{%cart}}".
 *
 */
class Cart extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%cart}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['buyer_id','store_id','goods_id','goods_price','goods_num'], 'required'],
            [['buyer_id','store_id','goods_id','goods_num'], 'integer'],
            [['goods_price'], 'number'],
            [['store_name','goods_name'], 'string', 'max' => 50],
            [['goods_image','goods_name'],'string','max'=>100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'cart_id' => 'ID',
            'buyer_id'=>'用户ID',
            'store_id'=>'店铺ID',
            'store_name' => '店铺名称',
            'goods_id' => '商品ID',
            'goods_name' => '商品名称',
            'goods_price' => '价格',
            'goods_num' => '数量',
            'goods_image' => '商品图片',
        ];
    }

    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'backendCreate' => ['goods_name', 'goods_code','goods_bar','goods_unit','gc_id','member_id','store_id'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }

    public function getGoods()
    {
        return $this->hasOne(Goods::className(),['goods_id'=>'goods_id'])
            ->select(['goods_id','goods_name','goods_image','status','goods_storage','goods_price','goods_marketprice']);
    }

    /**
     * @return array
     */
//    public function behaviors()
//    {
//        return [
//            [
//                'class' => TimestampBehavior::class,
//                'attributes' => [
//                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
//                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
//                ],
//            ],
//        ];
//    }
}
